<?php

//
// input type text
// 
function textfield($name, $value, $html) {
  echo "<input type='text' id='$name' name='$name' value='$value' $html />";
}


//
// textarea
// 
function textarea($name, $value, $html) {
  echo "<textarea id='$name' name='$name' $html>$value</textarea>";
}


//
// input type password
//
function passwordfield($name, $value, $html) {
  echo "<input type='password' id='$name' name='$name' value='$value' $html />";
}


//
// select
//
function simpleselect($name, $options, $selected, $has_blank, $html) {
  echo "<select id='$name' name='$name' $html >";

  // If $blank, we add a blank option
  if ($has_blank == true) echo '<option value=""></option>';

  // We iterate over $options to generate the <option> tags.
  foreach(array_keys($options) as $option_description) {

    // If the value of the option is leaved blank
    // We use the description as the value
    if (empty($options[$option_description]) || $options[$option_description] == null)
      $option_value = $option_description;
    else
      $option_value = $options[$option_description];
    
    // Here we check if the $option_value is the selected one.
    if ($option_value == $selected)
      echo "<option value='$option_value' selected> $option_description </option>";
    else
      echo "<option value='$option_value'> $option_description </option>";

  } // End foreach $options

  // End the select tag
  echo "</select>"; 
}


//
// select multiple
//
function multipleselect($name, $options, $selected, $has_blank, $html) {

  // Fix the this-selected property, should be an array
  if (is_string($selected)) {
    $selected = split(",", $selected);
  }

  // Remove brackets from the name, to make a string to be placed
  // on the id attribute.
  $name_without_brackets = str_replace("[]", "", $name);

  // We check if the $name ends with []
  // This is necessary to PHP handle the multiple select
  if (stripos($name, "[]") == false) {
    echo "<br/> 
          <b> Warning: </b> <i>$name</i> multiple select:
          The name should end in '[]' to PHP handle the multiple. 
          <br/>";
  }

  echo "<select multiple id='$name_without_brackets' 
                name='$name' $html>";


  // If $blank, we add a blank option
  if ($blank == true) echo '<option value=""></option>';

  // We iterate over $options to generate the <option> tags.
  foreach(array_keys($options) as $option_description) {
    // If the value of the option is leaved blank
    // We use the description as the value
    if (empty($options[$option_description]) || 
        $options[$option_description] == null) {
      $option_value = $option_description;
    } else {
      $option_value = $options[$option_description];
    }


    // Here we check if the $option_value is the selected one.
    if (in_array($option_value, $selected)) {
      echo "<option value='$option_value' selected> 
              $option_description 
            </option>";
    } else {
      echo "<option value='$option_value'> 
              $option_description 
            </option>";
    }
  } // End foreach $options


  // End the select tag
  echo "</select>";  
}

?>
